'''Copyright 2009 Marcus Wanner, Colin Rooney

This file is part of the Python Debug Logging Suite.

The Python Debug Logging Suite is free software: you can redistribute
it and/or modify it under the terms of the GNU General Public License
as published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.

The Python Debug Logging Suite is distributed in the hope that it will
be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with The Python Debug Logging Suite.  If not, see
<http://www.gnu.org/licenses/>.

This file is the basic module for file logging.'''

import os, time
time.clock() #needs so that the timestamp is in consistent units, don't ask why

class logobj():
    'Object to handle logfile. Each time a new one is made, the logfile is erased.'
    def __init__(self):
        'Initializes the logfile.'
        self._valid = 1 #object is valid at start
        self.timestamp = 0 #timestamp is off by default. Should we change this?
        self._logfile = open('debug.log', 'w') #open logfile
        print 'Debug log will be saved to: '+os.getcwd()+os.sep+'debug.log' #print a helpful message about log location

    def _checkvalid(self):
        if self._valid == 0: #function for this in case we want to change it
            raise NameError, 'Object is no longer valid.'

    def ping():
        self._checkvalid()
        print "WARNING: You can't ping a logfile!" #just has to be here...
    
    def msg(self, msgtext):
        'Use to write message to logfile. Input can be any type.'
        self._checkvalid()
        if self.timestamp == 1:
            msgtext = str(time.clock()) + ': ' + str(msgtext)

        self._logfile.write(str(msgtext) + '\n') #write message to logfile

    def close(self):
        'Destroys the object (sort of).'
        self._checkvalid() #don't what to close the logfile twice...
        self._logfile.close() #close the logfile
        self._valid = 0

    def __del__(self):
        self._logfile.close()
